#include "playscene.h"
#include<QDebug>
#include<QMenuBar>
#include<QPainter>
#include<mypushbutton.h>
#include<QTimer>
#include<QLabel>
#include<mycoin.h>
#include<QPropertyAnimation>
#include<dataconfig.h>
#include<QSound>

//PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
//{

//}

PlayScene::PlayScene(int levelNum){
    QString str= QString("进入了第 %1 关").arg(levelNum);
    qDebug()<<str;
    this->levelIndex=levelNum;

    //初始化游戏场景
    this->setFixedSize(390,640);
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    this->setWindowTitle("哎呀，这玩翻金币，多是一件美逝啊！");

    //创建菜单栏
    QMenuBar *bar=menuBar();
    setMenuBar(bar);

    //创建开始菜单
    QMenu *startMenu= bar->addMenu("开始");

    //创建退出项
    QAction * quitAction=startMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //开始背景音效创建
    QSound *startBkSound =new QSound(":/res/JT03play.wav",this);
    startBkSound->play();
    //结束背景音效创建
    QSound *startBkSound01 =new QSound(":/res/JT04end.wav",this);


    //返回按钮音效创建
    QSound *backSound =new QSound(":/res/BackButtonSound.wav",this);
    //金币音效创建
    QSound *coinSound =new QSound(":/res/ConFlipSound.wav",this);
    //胜利音效创建
    QSound *winSound =new QSound(":/res/LevelWinSound.wav",this);

    //返回按钮
    MyPushButton *backbtn=new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backbtn->setParent(this);
    backbtn->move(this->width()-backbtn->width(),this->height()-backbtn->height());
    connect(backbtn,&MyPushButton::clicked,[=](){
        //返回音效
        backSound->play();

        startBkSound->stop();
        startBkSound01->stop();

        //qDebug()<<"点击了返回按钮";
        //告诉选关场景 我返回了，选关场景监听返回按钮
        //延时返回
        QTimer::singleShot(300,this,[=](){
            emit this->playSceneBack();
        });
    });

    //显示当前关卡
    QLabel *label =new QLabel;
    label->setParent(this);
    QFont font;
    font.setFamily("宋体");
    font.setPixelSize(30);
    QString str1 =QString("Level: %1").arg(this->levelIndex);
    //将字体设置在标签控件
    label->setFont(font);
    label->setText(str1);
    label->setGeometry( 30 ,this->height()-75, 150 , 50 );

    dataConfig config;
    //初始化每关二维数组
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            this->gameArray[i][j]=config.mData[this->levelIndex][i][j];
        }
    }

    //胜利图片显示
    QLabel *winLabel=new QLabel;
    QPixmap tmpPix;
    tmpPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
    winLabel->setPixmap(tmpPix);
    winLabel->setParent(this);
    winLabel->move((this->width()-tmpPix.width())/2,-tmpPix.height());//预置在视野外

    QLabel *winRules=new QLabel;
    winRules->setText("胜利条件：将所有硬币翻为金币状态");
    winRules->setParent(this);
    winRules->setGeometry( 30 ,75, 200 , 50 );
    winRules->move((this->width()-winRules->width())/2,150);



    //显示金币背景
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            //绘制背景图片
            QPixmap pix=QPixmap(":/res/BoardNode.png");
            QLabel *label=new QLabel;
            label->setGeometry(0,0,pix.width(),pix.height());
            label->setPixmap(pix);
            label->setParent(this);
            label->move(80+i*60,220+j*60);

            //创建金币
            QString str;
            if(gameArray[i][j]==1){
                //显示金币
                str=":/res/Coin0001.png";
            }
            else{
                str=":/res/Coin0008.png";
            }

            MyCoin *coin =new MyCoin(str);
            coin->setParent(this);
            coin->move(82+i*60,223+j*60);

            //属性赋值
            coin->posX=i;
            coin->posY=j;
            coin->flag=this->gameArray[i][j];//1正 0反

            //将金币放入二维数组，以便维护
            coinBtn[i][j]=coin;

            //点击 进行翻转
            connect(coin,&MyCoin::clicked,[=](){
                coinSound->play();
                //禁用所有按钮
                for(int i=0;i<4;i++){
                    for(int j=0;j<4;j++){
                        coinBtn[i][j]->isWin=true;
                    }
                }

                coin->changeFlag();
                this->gameArray[i][j]=this->gameArray[i][j]== 0 ? 1:0;

                //翻转周围，延时翻转
                QTimer::singleShot(300,this,[=](){
                    if(coin->posX+1<=3){//右侧可翻转
                        coinBtn[coin->posX+1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX+1][coin->posY]=this->gameArray[coin->posX+1][coin->posY]== 0 ? 1:0;
                    }
                    if(coin->posX-1>=0)//左侧
                    {
                        coinBtn[coin->posX-1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX-1][coin->posY]=this->gameArray[coin->posX-1][coin->posY]== 0 ? 1:0;
                    }
                    if(coin->posY+1<=3){//上侧可翻转
                        coinBtn[coin->posX][coin->posY+1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY+1]=this->gameArray[coin->posX][coin->posY+1]== 0 ? 1:0;
                    }
                    if(coin->posY-1>=0){//下侧可翻转
                        coinBtn[coin->posX][coin->posY-1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY-1]=this->gameArray[coin->posX][coin->posY-1]== 0 ? 1:0;
                    }

                    //翻完周围金币后，解开周围禁用
                    for(int i=0;i<4;i++){
                        for(int j=0;j<4;j++){
                            coinBtn[i][j]->isWin=false;
                        }
                    }

                    //判断是否胜利
                    this->isWin=true;
                    for(int i=0;i<4;i++){
                        for(int j=0;j<4;j++){
                            if(coinBtn[i][j]->flag==false){//只要有一个反面，就不成功
                                this->isWin=false;
                                break;
                            }
                        }
                    }

                    if(this->isWin==true){
                        qDebug()<<"游戏胜利";
                        startBkSound->stop();
                        winSound->play();

                        //将所有按钮标志改为true
                        //胜利后不可再次点击
                        for(int i=0;i<4;i++){
                            for(int j=0;j<4;j++){
                                coinBtn[i][j]->isWin=true;
                            }
                        }

                        //胜利标志出现
                        QPropertyAnimation *animation=new QPropertyAnimation(winLabel,"geometry");
                        //设置时间间隔
                        animation->setDuration(1000);
                        //开始结束位置和曲线
                        animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                        animation->setEndValue(QRect(winLabel->x(),winLabel->y()+114,winLabel->width(),winLabel->height()));
                        animation->setEasingCurve(QEasingCurve::OutBounce);

                        //执行动画
                        animation->start();
                        this->setWindowTitle("啊哈哈哈，我滴任务完成啦！");


                        startBkSound01->play();
                    }
                });
            });

        }
    }



}

void PlayScene::paintEvent(QPaintEvent *){

    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //标题
    pix.load(":/res/Title.png");
    painter.drawPixmap( 10 ,30,pix.width(),pix.height(),pix);
}
